/* 
 * JUGJobs -- A Jobs Posting Application for Java Users Groups and Other Groups
 * SaveJobAction.java,v 1.2 2005/10/04 03:08:22 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JUGJobs project.
 *
 * The Initial Developers of the Original Code are the members of the Triangle
 * Java User's Group in the RTP area of North Carolina.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developers. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.jugjobs.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jugjobs.form.JobBean;
import org.jugjobs.model.JobList;

/**
 * @author BigLee
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SaveJobAction extends CommonAction 
{

	  public ActionForward execute(ActionMapping mapping, ActionForm form,
	      HttpServletRequest request, HttpServletResponse response)
	      throws Exception
	  {
	    JobList modelBean = getModelBean(servlet);
	    JobBean entryForm = (JobBean)form;
        int jobNumber = Integer.parseInt(entryForm.getJob_number());

	    if (isCancelled(request))
	    { //saveErrors(request, new ActionErrors());
	      return mapping.findForward("jobList"); 
       	}
	  	
 	    String buttonValue = request.getParameter("button");
	    if(buttonValue != null && "Delete job".equals(buttonValue))
   		{ 
	      logger.log("Deleting job number " + jobNumber + "f/ SaveJobAction");
	      modelBean.deletePosting(jobNumber);
	      return mapping.findForward("jobList");
	    }

        boolean changedJob = (jobNumber != -1);
        HttpSession session = request.getSession();
	  
        if(changedJob && (!isUserAuthorizedForJob(session, jobNumber)))
	      { // something is wrong; usual reason for this is, having logged in,
        	// user lets things sit for too long and the session times out.
	        logger.log("In SaveJobAction, user not authorized - timeout?");
	        ActionErrors errors = new ActionErrors();
	        errors.add("session", new ActionError("jugjobs.savejob.text.nosession"));
	        saveErrors(request, errors);
	        return mapping.getInputForward();
	      }
	    
        if (changedJob)
        { logger.log("In SaveJobAction, " +
	            "Replacing (Updating) job number " + jobNumber + 
	            " has been requested.");
	        modelBean.replacePosting(entryForm);
	        session.removeAttribute(AUTHENTICATED_FOR_JOB_NO);
	        return mapping.findForward("jobList");
        }
        else
        { logger.log("In SaveJobAction, " +
	          "Entering a new job has been requested.");
	      modelBean.insertPosting(entryForm);
	      return mapping.findForward("jobList");
	    }
	  }
	}
